package com.example.rzprogram;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.example.adapter.HomeAdapter;
import com.example.adapter.MyPagerAdapter;
import com.example.base.BaseActivity;
import com.example.bean.Image;
import com.example.bean.News;
import com.example.bean.NewsBean;
import com.example.network.ProtocolConst;
import com.example.network.ProtocolService;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.slidingmenu.lib.SlidingMenu;

public class MainActivity extends BaseActivity implements OnItemClickListener, OnPageChangeListener {
	@ViewInject(R.id.btn_yimin)
	private TextView tv_yimin;
	@ViewInject(R.id.btn_apperance)
	private TextView tv_apperance;
	@ViewInject(R.id.btn_canada)
	private TextView tv_canada;
	@ViewInject(R.id.btn_success)
	private TextView tv_sccess;
	@ViewInject(R.id.btn_order)
	private TextView tv_order;
	@ViewInject(R.id.btn_about)
	private TextView tv_about;
	@ViewInject(R.id.btn_menu)
	private TextView tv_menu;
	@ViewInject(R.id.btn_percenter)
	private Button pecennter;
	@ViewInject(R.id.btn_percenter)
	private Button btn_per;
	@ViewInject(R.id.lay_dot)
	private LinearLayout layout;
	@ViewInject(R.id.home_list)
	private ListView listView;
	@ViewInject(R.id.viewpager)
	private ViewPager viewPager;
	@ViewInject(R.id.btn_message)
	private Button btn_msg;
	@ViewInject(R.id.btn_zixun)
	private Button btn_zixun;
			
	private HomeAdapter adapter;
	private Context context;
	private SlidingMenu menu;

	private List<News> list = new ArrayList<News>();
	private ArrayList<ImageView> list2 = new ArrayList<ImageView>();
	private ArrayList<ImageView> dots = new ArrayList<ImageView>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		ViewUtils.inject(this);
		btn_per.setVisibility(View.GONE);
		context = this;

		dot(0);

		// 实例化滑动菜单对象
		menu = new SlidingMenu(this);
		// 设置为左滑菜单
		menu.setMode(SlidingMenu.LEFT);
		// 设置触摸屏幕的模式
		menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
		// 设置滑动阴影的宽度
		menu.setShadowWidthRes(R.dimen.shadow_width);
		// 设置滑动阴影的图像资源
		menu.setShadowDrawable(R.drawable.shadow);
		// 设置滑动菜单划出时主页面显示的剩余宽度
		menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		// 设置渐入渐出效果的值
		menu.setFadeDegree(0.35f);
		// 附加在Activity上
		menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
		// 设置滑动菜单的布局
		menu.setMenu(R.layout.slidingmenu);

		// pager图片加载
		ProtocolService.image("1", new RequestCallBack<String>() {
			
			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				// 接口回调成功
				Image bean = JSONObject.parseObject(arg0.result, Image.class);
				BitmapUtils utils = new BitmapUtils(context);
				for(int i=0;i<bean.getImagelist().size();i++){
					//设置小圆点
					ImageView dot_view = new ImageView(context);
					dot_view.setImageResource(R.drawable.dot1);
					dot_view.setPadding(10, 10, 10, 10);
					layout.addView(dot_view);
					ImageView view = new ImageView(context);
					dots.add(dot_view);
					//获取图片资源
					utils.display(view, ProtocolConst.MAIN+bean.getImagelist().get(i).getImageName());
					list2.add(view);
				}
				viewPager.setAdapter(new MyPagerAdapter(context, list2));
			}
			
			@Override
			public void onFailure(HttpException arg0, String arg1) {
				// 接口回调失败
			}
		});
		// listview数据加载
		ProtocolService.newsList("1", "10", "0", new RequestCallBack<String>() {

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				// 接口回调成功
				NewsBean bean = JSONObject.parseObject(arg0.result, NewsBean.class);
				list = bean.getNewslist();
				adapter = new HomeAdapter(list, context);
				listView.setAdapter(adapter);
				// 设置viewpager预加载的page的个数的限制
				viewPager.setOffscreenPageLimit(2);
				viewPager.setOnPageChangeListener(MainActivity.this);
			}

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				// 接口回调失败
			}
		});
	}
	// 小圆点切换
	private void dot(int index) {
		for (int i = 0; i < list2.size(); i++) {
			if (index == i) {
				dots.get(i).setAlpha(255);
			} else {
				dots.get(i).setAlpha(100);
			}
		}
	}

	// 当滑动状态改变时调用
	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	// 当当前页面被滑动时调用
	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	// 当新的页面被选中时调用
	@Override
	public void onPageSelected(int arg0) {
		// TODO Auto-generated method stub
		dot(arg0);
	}

	@OnClick({ R.id.btn_menu, R.id.btn_percenter, R.id.btn_yimin,R.id.btn_apperance,
		R.id.btn_canada,R.id.btn_about,R.id.btn_success,R.id.btn_order ,R.id.btn_message,R.id.btn_zixun})
	public void onCliecks(View v) {
		switch (v.getId()) {
		case R.id.btn_menu:
			// 点击菜单按钮
			menu.showMenu();
			break;
		case R.id.btn_zixun:
			// 用intent启动拨打电话
			Intent intents = new Intent(
			Intent.ACTION_CALL, Uri.parse("tel:" + "58287168"));
			startActivity(intents);
			break;
		case R.id.btn_message:
			Intent intent0 = new Intent(MainActivity.this, MessageActivity.class);
			startActivity(intent0);
			break;
		case R.id.btn_yimin:
			Intent intent2 = new Intent(MainActivity.this, YiMinActivity.class);
			intent2.putExtra("imageType", 2);
			intent2.putExtra("newsType", 1);
			startActivity(intent2);
			break;
		case R.id.btn_apperance:
			Intent intent3 = new Intent(MainActivity.this, YiMinActivity.class);
			intent3.putExtra("imageType", 3);
			intent3.putExtra("newsType", 2);
			startActivity(intent3);
			break;
		case R.id.btn_canada:
			Intent intent4 = new Intent(MainActivity.this, YiMinActivity.class);
			intent4.putExtra("imageType", 4);
			intent4.putExtra("newsType", 3);
			startActivity(intent4);
			break;
		case R.id.btn_success:
			Intent intent5 = new Intent(MainActivity.this, YiMinActivity.class);
			intent5.putExtra("imageType", 5);
			intent5.putExtra("newsType", 4);
			startActivity(intent5);
			break;
		case R.id.btn_order:
			Intent intent6 = new Intent(MainActivity.this, YiMinActivity.class);
			intent6.putExtra("imageType", 6);
			intent6.putExtra("newsType", 0);
			startActivity(intent6);
			break;
		case R.id.btn_about:
			Intent intent7 = new Intent(MainActivity.this, AboutActivity.class);
			startActivity(intent7);
			break;
		}
	}

	// listView 点击事件
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Intent intent = new Intent(this, ZixunDelActivity.class);
		startActivity(intent);
	}

}
